/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.comac.ins.common.core.exception.base.BaseException;
import com.comac.ins.common.core.utils.MapstructUtils;
import com.comac.ins.common.core.utils.StringUtils;
import com.comac.ins.common.mybatis.core.page.PageQuery;
import com.comac.ins.common.mybatis.core.page.TableDataInfo;
import com.comac.ins.common.oss.constant.OssConstant;
import com.comac.ins.common.oss.core.OssClient;
import com.comac.ins.common.oss.factory.OssFactory;
import com.comac.ins.isfd.constant.enums.IsfdSectionTypeEnum;
import com.comac.ins.isfd.domain.bo.*;
import com.comac.ins.isfd.domain.bo.query.*;
import com.comac.ins.isfd.domain.thirdParty.*;
import com.comac.ins.isfd.domain.vo.*;
import com.comac.ins.isfd.factory.IsfdSectionFactory;
import com.comac.ins.isfd.service.*;
import com.comac.ins.isfd.util.ThirdPartyUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;

@RequiredArgsConstructor
@Service
@Slf4j
public class IsfdThirdPartyAccessServiceImpl implements IIsfdThirdPartyAccessService {

    @Autowired
    private ThirdPartyUtils thirdPartyUtils;

    @Autowired
    private IIsfdMetalMaterialStaticStrengthAnalysisService isfdMetalMaterialStaticStrengthAnalysisService;

    @Autowired
    private IIsfdLayupSkinPropertieService layupSkinPropertieService;

    @Autowired
    private IIsfdLayupMetalCaseMetaService layupMetalCaseMetaService;

    @Autowired
    private IsfdSectionFactory isfdSectionFactory;

    @Autowired
    private IIsfdModelGeneralPreprocessService iIsfdModelGeneralPreprocessService;

    @Autowired
    private IIsfdResultGeneralPreprocessService iIsfdResultGeneralPreprocessService;

    @Autowired
    private IIsfdLoadExperimentService iIsfdLoadExperimentService;

    @Autowired
    private IIsfdModelService iIsfdModelService;

    @Autowired
    private IIsfdModelParametricService iIsfdModelParametricService;

    @Lazy
    @Autowired
    private IIsfdMeshModelStructureDefinitionService iIsfdMeshModelStructureDefinitionService;

    @Lazy
    @Autowired
    private IIsfdCollectBatchHistoryService iIsfdCollectBatchHistoryService;

    @Lazy
    @Autowired
    private IIsfdCollectRoundHistoryService iIsfdCollectRoundHistoryService;

    @Override
    public TableDataInfo<IsfdMetalMaterialStaticStrengthAnalysisThirdPartyVo> staticStrengthAnalysisMatList(IsfdMetalMaterialStaticStrengthAnalysisQueryBo isfdMetalMaterialStaticStrengthAnalysisQueryBo) {
        TableDataInfo<IsfdMetalMaterialStaticStrengthAnalysisVo> result = isfdMetalMaterialStaticStrengthAnalysisService.queryPageList(isfdMetalMaterialStaticStrengthAnalysisQueryBo);
        long total = result.getTotal();
        if (total > 0) {
            List<IsfdMetalMaterialStaticStrengthAnalysisVo> rows = result.getRows();
            List<IsfdMetalMaterialStaticStrengthAnalysisThirdPartyVo> thirdPartResult = MapstructUtils.convert(rows, IsfdMetalMaterialStaticStrengthAnalysisThirdPartyVo.class);
            return TableDataInfo.build(thirdPartResult, total);
        }
        return TableDataInfo.build();
    }

    @Override
    public IsfdLayupSkinPropertieVo queryLayupSkinPropertie(IsfdLayupSkinPropertieThridPartyBo bo) {
        if (StringUtils.isBlank(bo.getPlyCode())) {
            throw new BaseException("铺层代码不能为空！");
        }
        // 构造查询
        IsfdLayupSkinPropertieQueryBo queryBo = new IsfdLayupSkinPropertieQueryBo();
        queryBo.setPlyCode(bo.getPlyCode());
        queryBo.setPageNum(1);
        queryBo.setPageSize(20);
        // 先查询，能不能查到
        TableDataInfo<IsfdLayupSkinPropertieVo> vos = layupSkinPropertieService.queryPageList(queryBo);
        // 查到直接返回
        if (!CollectionUtil.isEmpty(vos.getRows())) {
            return vos.getRows().get(0);
        }
        // 新增并返回
        IsfdLayupSkinPropertieBo propertieBo = MapstructUtils.convert(bo, IsfdLayupSkinPropertieBo.class);
        propertieBo.setPlyData(bo.getData());
        layupSkinPropertieService.save(propertieBo);
        vos = layupSkinPropertieService.queryPageList(queryBo);
        if (CollectionUtil.isEmpty(vos.getRows())) {
            throw new BaseException("蒙皮新增失败");
        }
        return vos.getRows().get(0);
    }

    @Override
    public IsfdLayupMetalCaseMetaVo queryLayupMetalCase(IsfdLayupMetalCaseMetaBo bo) {
        // 参数检查
        thirdPartyUtils.checkMetalCaseParams(bo);
        IsfdLayupMetalCaseMetaQueryBo queryBo = new IsfdLayupMetalCaseMetaQueryBo();
        queryBo.setMaterialCode(bo.getMaterialCode());
        queryBo.setSectionNumber(bo.getSectionNumber());
        queryBo.setThickness(bo.getThickness());
        TableDataInfo<IsfdLayupMetalCaseMetaVo> vos = layupMetalCaseMetaService.queryPageList(queryBo);

        if (!CollectionUtil.isEmpty(vos.getRows())) {
            return vos.getRows().get(0);
        }

        layupMetalCaseMetaService.insertByBo(bo);
        vos = layupMetalCaseMetaService.queryPageList(queryBo);
        if (CollectionUtil.isEmpty(vos.getRows())) {
            throw new BaseException("金属壳单元新增失败");
        }
        return vos.getRows().get(0);
    }

    @Override
    public IsfdSectionVo querySectionProperty(IsfdSectionThirdPartyBo bo) {
        String type = bo.getType();
        IsfdSectionTypeEnum isfdSectionTypeEnum = IsfdSectionTypeEnum.forValue(type);
        if (isfdSectionTypeEnum == null) {
            throw new BaseException("型材类型不存在，请确认后重试！");
        }
        IIsfdSectionService sectionService = isfdSectionFactory.getSectionServiceByType(isfdSectionTypeEnum);
        IsfdSectionBo sectionBo = MapstructUtils.convert(bo, IsfdSectionBo.class);
        // 生成编号
        String serialNumber = sectionService.generateSerialNumber(sectionBo);
        sectionBo.setSerialNumber(serialNumber);
        // 根据编号查询
        IsfdSectionQueryBo queryBo = new IsfdSectionQueryBo();
        queryBo.setType(bo.getType());
        queryBo.setSerialNumber(serialNumber);
        TableDataInfo<IsfdSectionVo> voTableDataInfo = sectionService.queryPage(queryBo);
        if (!CollectionUtil.isEmpty(voTableDataInfo.getRows())) {
            return voTableDataInfo.getRows().get(0);
        }

        sectionService.save(sectionBo);

        voTableDataInfo = sectionService.queryPage(queryBo);
        if (CollectionUtil.isEmpty(voTableDataInfo.getRows())) {
            throw new BaseException("型材数据新增失败！");
        }

        return voTableDataInfo.getRows().get(0);
    }

    @Override
    public Boolean saveParametricModel(IsfdModelParametricBo bo) {
        thirdPartyUtils.checkModelParametricBo(bo);
        String filePath = bo.getFilePath();
        // 校验文件是否存在
        OssClient storage = OssFactory.instance(OssConstant.PREPROCESSING_CONFIG_KEY);
        boolean fileExist = storage.doesUrlExist(filePath);
        if (!fileExist) {
//            throw new BaseException("OSS中未找到文件：" + filePath);
        }
        iIsfdModelParametricService.insertByBo(bo);
        return Boolean.TRUE;
    }

    @Override
    public TableDataInfo<IsfdModelParametricVo> queryParametricModelList(IsfdModelParametricQueryThirdPartyBo queryBo) {
        IsfdModelParametricBo isfdModelParametricBo = MapstructUtils.convert(queryBo, IsfdModelParametricBo.class);
        PageQuery pageQuery = new PageQuery();
        pageQuery.setPageNum(queryBo.getPageNum());
        pageQuery.setPageSize(queryBo.getPageSize());
        return iIsfdModelParametricService.queryPageList(isfdModelParametricBo, pageQuery);
    }


    @Override
    public Boolean savePreprocessModel(IsfdModelGeneralPreprocessBo bo) {
        thirdPartyUtils.checkModelGeneralPreprocessBo(bo);
        String filePath = bo.getFilePath();
        // 校验文件是否存在
        OssClient storage = OssFactory.instance(OssConstant.PREPROCESSING_CONFIG_KEY);
        boolean fileExist = storage.doesUrlExist(filePath);
        if (!fileExist) {
//            throw new BaseException("OSS中未找到文件：" + filePath);
        }
        iIsfdModelGeneralPreprocessService.insertByBo(bo);
        return Boolean.TRUE;
    }

    @Override
    public TableDataInfo<IsfdModelGeneralPreprocessVo> queryPreprocessModelList(IsfdModelGeneralPreprocessQueryThirdPartyBo queryBo) {
        IsfdModelGeneralPreprocessBo isfdModelGeneralPreprocessBo = MapstructUtils.convert(queryBo, IsfdModelGeneralPreprocessBo.class);
        PageQuery pageQuery = new PageQuery();
        pageQuery.setPageNum(queryBo.getPageNum());
        pageQuery.setPageSize(queryBo.getPageSize());
        return iIsfdModelGeneralPreprocessService.queryPageList(isfdModelGeneralPreprocessBo, pageQuery);
    }

    @Override
    public Boolean savePreprocessResult(IsfdResultGeneralPreprocessBo bo) {
        thirdPartyUtils.checkResultGeneralPreprocessBo(bo);
        String url = bo.getUrl();
        // 校验文件是否存在
        OssClient storage = OssFactory.instance(OssConstant.PREPROCESSING_CONFIG_KEY);
        boolean fileExist = storage.doesUrlExist(url);
        if (!fileExist) {
//            throw new BaseException("OSS中未找到文件：" + url);
        }
        iIsfdResultGeneralPreprocessService.insertByBo(bo);
        return Boolean.TRUE;
    }

    @Override
    public TableDataInfo<IsfdResultGeneralPreprocessVo> queryPreprocessResultList(IsfdResultGeneralPreprocessQueryThirdPartyBo queryBo) {
        IsfdResultGeneralPreprocessBo bo = MapstructUtils.convert(queryBo, IsfdResultGeneralPreprocessBo.class);
        PageQuery pageQuery = new PageQuery();
        pageQuery.setPageNum(queryBo.getPageNum());
        pageQuery.setPageSize(queryBo.getPageSize());
        return iIsfdResultGeneralPreprocessService.queryPageList(bo, pageQuery);
    }

    @Override
    public TableDataInfo<IsfdMeshModelStructureDefinitionVo> meshModelStructureDefinitionList(IsfdMeshModelStructureDefinitionsQueryThirdPartyBo queryThirdPartyBo) {
        thirdPartyUtils.checkStructureDefinitionsQueryParams(queryThirdPartyBo);
        IsfdMeshModelStructureDefinitionQueryBo queryBo = new IsfdMeshModelStructureDefinitionQueryBo();
        queryBo.setLevel(queryThirdPartyBo.getLevel());
        queryBo.setParentId(queryThirdPartyBo.getParentId());
        queryBo.setPageNum(queryThirdPartyBo.getPageNum());
        queryBo.setPageSize(queryThirdPartyBo.getPageSize());
        return iIsfdMeshModelStructureDefinitionService.queryPageList(queryBo);
    }


    @Override
    public List<String> collectBatchHistoryList(String modelNumber) {
        return iIsfdCollectBatchHistoryService.batchHistoryList(modelNumber);
    }

    @Override
    public List<String> collectRoundHistoryList(String modelNumber) {
        return iIsfdCollectRoundHistoryService.roundHistoryList(modelNumber);
    }

    @Override
    public Boolean saveModelInfo(IsfdModelBo bo) {
        return iIsfdModelService.insertByBo(bo);
    }

    @Override
    public Boolean loadExperimentSave(IsfdLoadExperimentThridPartyBo bo) {
        IsfdLoadExperimentBo isfdLoadExperimentBo = MapstructUtils.convert(bo, IsfdLoadExperimentBo.class);
        isfdLoadExperimentBo.setRoundNumber(bo.getRound());
        isfdLoadExperimentBo.setModelNumber(bo.getModelType());
        return iIsfdLoadExperimentService.save(isfdLoadExperimentBo);
    }
}
